07. Exercise: Working with Objects
Title
Exercise: Working with Objects
ND079 C1 L2 A07b Person Class
Defining a Person Class
Task Description:
In this exercise you will be creating and instantiating objects. First, we will define a Person
class like we saw in the example earlier.
Task Feedback:
Nice work!
Testing our Person Class
ND079 C1 L2 A07c Testing Our Person Class
Creating a PersonTester
Task Description:
Next, let's create a class that has a main
method from which we can instantiate the Person
class.
Task Feedback:
Nice work!
ND079 C1 L2 A07d Code Demo
Solution
Solution
public class Person {
private String firstName;
private String lasName;
public Person(String firstName, String lasName) {
this.firstName = firstName;
this.lasName = lasName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLasName() {
return lasName;
}
public void setLasName(String lasName) {
this.lasName = lasName;
}
@Override
public String toString() {
return "Name:" + firstName + " " + lasName;
}
}
public class PersonTester {
public static void main(String[] args) {
Person bob = new Person("Bob", "Phillips");
Person mike = new Person("Mike", "Lipson");
System.out.println(bob);
System.out.println(mike);
}
}